package sh.wn36.vehiclemanage.service.impl;

import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import sh.wn36.vehiclemanage.entity.Dictionary;
import sh.wn36.vehiclemanage.entity.QUsers;
import sh.wn36.vehiclemanage.entity.qz_entity.Users;
import sh.wn36.vehiclemanage.mapper.QUsersMapper;
import sh.wn36.vehiclemanage.service.DictionaryService;
import sh.wn36.vehiclemanage.service.QUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import sh.wn36.vehiclemanage.util.MD5Util;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xk
 * @since 2021-04-26
 */
@Service
public class QUsersServiceImpl extends ServiceImpl<QUsersMapper, QUsers> implements QUsersService {

    @Autowired
    @ApiModelProperty("部门服务接口")
    private DictionaryService dictionaryService;

    @Autowired
    @ApiModelProperty("用户接口")
    private QUsersMapper usersMapper;
    @Override
    public List<Users> UsersList() {
        List<QUsers> usersList = this.list();
//        List<Dictionary> dictionaryList = dictionaryService.list();
        List<Users> users = new ArrayList<>();
        for (QUsers qUsers : usersList) {
//  同过用户的职位id查询出职位详细信息
            Dictionary position = dictionaryService.getById(qUsers.getPostid());
            //            获取用户的部门编号 和名称
            Dictionary department = dictionaryService.getById(position.getPid());
            Users users1 = new Users();
            users1.setAddress(qUsers.getAddress()); users1.setLname(qUsers.getLname());; users1.setId(qUsers.getId()); users1.setLpass(qUsers.getLpass()); users1.setRname(qUsers.getRname());
            users1.setAge(qUsers.getAge()); users1.setPhone(qUsers.getPhone()); users1.setDepartment(department.getText()); users1.setPosition(position.getText()); users1.setBirthday(qUsers.getBirthday());
            users1.setCard(qUsers.getCard()); users1.setEntrytime(qUsers.getEntrytime()); users1.setIsdisable(qUsers.getIsdisable()==1?"启用":"禁用"); users1.setVersion(qUsers.getVersion()); users1.setGmtCreate(qUsers.getGmtCreate()); users1.setGmtModified(qUsers.getGmtModified());
            users.add(users1);
        }
        System.out.println("用户服务实现层封装好的用户集合"+users);

        if (users!=null){
            return users;
        }
        return  null;
    }

    @ApiModelProperty("根据用户id重置密码")
    @Override
    public boolean initPwdById( String id) {
        String pwd = MD5Util.md5Encrypt32Lower("123456");

        int i = usersMapper.initPwdByid(pwd, Integer.parseInt(id));
        if(i>0){
            return  true;
        }
        return false;
    }

    @Override
    public QUsers loginUser(String username) {
       return  usersMapper.loginUser(username);
    }

    @Override
    public boolean updatePassword(Integer id, String rpass) {
        int i = usersMapper.updatePassword(id, MD5Util.md5Encrypt32Lower(rpass));
        if(i>0){
            return true;
        }
        return false;
    }
}
